package com.jinghao.tws.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.jinghao.core.common.exception.JingHaoCustomerException;
import com.jinghao.core.common.service.impl.JingHaoBaseServiceImpl;
import com.jinghao.core.common.util.JhHttpClientUtil;
import com.jinghao.core.common.util.cnd.JhCnd;
import com.jinghao.tws.entity.WvAccessConfig;
import com.jinghao.tws.service.IWvAccessConfigService;
import com.jinghao.tws.util.EJhTwsCode;

@Service("wvAccessConfigService")
public class WvAccessConfigServiceImpl extends JingHaoBaseServiceImpl<WvAccessConfig> implements IWvAccessConfigService {
	//==================定制内容开始======================
	//在这里修改代码
	@Override
	public void getWvAccessConfig(WvAccessConfig model) {
		model.setMapList(this.queryMapList(model));
		
	}
	public void checkWvAccessConfigInfo(WvAccessConfig model){
		Map<String,Object> map=model.getObjMap();
		if (map.get("accessUuid")==null||StringUtils.isEmpty(map.get("accessUuid").toString())) {
			
		}else{
			model.setAccessUuid(map.get("accessUuid").toString());
		}
		if (map.get("address")==null||StringUtils.isEmpty(map.get("address").toString())) {
			throw new JingHaoCustomerException(EJhTwsCode.TWS1001.getCode(),"请输入VMS IP地址！");
		}
		model.setAddress(map.get("address").toString());
		if (map.get("username")==null||StringUtils.isEmpty(map.get("username").toString())) {
			throw new JingHaoCustomerException(EJhTwsCode.TWS1001.getCode(),"请输入VMS 用户名 ！");
		}
		model.setUsername(map.get("username").toString());
		if (map.get("password")==null||StringUtils.isEmpty(map.get("password").toString())) {
			throw new JingHaoCustomerException(EJhTwsCode.TWS1001.getCode(),"请输入VMS 用户密码！");
		}
		model.setPassword(map.get("password").toString());
		if (map.get("bucket")==null||StringUtils.isEmpty(map.get("bucket").toString())) {
			throw new JingHaoCustomerException(EJhTwsCode.TWS1001.getCode(),"请输入连接标识！");
		}
		model.setBucket(map.get("bucket").toString());
		if(map.get("remark")==null){
			model.setRemark("");
		}else{
			model.setRemark(map.get("remark").toString());
		}
	}
	@Override
	public void addWvAccessConfig(WvAccessConfig model) {
		if(null!=model.getObjMap()){
			this.checkWvAccessConfigInfo(model);
		}else{
			throw new JingHaoCustomerException(EJhTwsCode.TWS1001.getCode(),"请求参数有误 ！");
		}
		List<Map<String,Object>> list=this.queryMapList(model);
		List<String> strList=new ArrayList<String>();
		for(Map<String,Object> map:list){
			strList.add(map.get("accessUuid").toString());
		}
		//删除历史数据
		if(list!=null&&list.size()>0){
			JhCnd cnd=new JhCnd();
			model.setStrList(strList);
			cnd.andIn("accessUuid", "strList", strList);
			model.setJhCnd(cnd);
			this.deleteSame(model);
		}
		//保存数据
		model.setAccessUuid(model.getUUID());
		this.add(model);
	}
	@Override
	public WvAccessConfig getWvAccessConfigInfo() {
		/*String userName="netease";
		String password="netease123";
		String dtUrl="netease";
		String httpUrl="http://kaiupcn.oicp.net:8091/api/"+dtUrl;
		*/
		WvAccessConfig model=new WvAccessConfig();
		List<WvAccessConfig> list=this.queryModelList(model);
		if(list==null||list.size()<=0){
			throw new JingHaoCustomerException(EJhTwsCode.TWS1002.getCode(),"未找到相关配置信息！");
		}
		return list.get(0);
	}
	public String vmsLogin(String userName,String password,String httpUrl){
		String loginUrl=httpUrl+"/login";
		Map<String,String> map=new HashMap<String,String>();
		JhHttpClientUtil hd = new JhHttpClientUtil();
		map.put("user-name", userName);
		map.put("password", password);
        String msg= hd.post(loginUrl, map);
        Map<String,Object> resultMap=(Map<String,Object>)JSON.parseObject(msg);
        return  resultMap.get("session-key").toString();
	}
	
	public Map<String,Object> vmsRequestconcatenatevideo(String httpUrl,String sessionKey,String mediaInfo){
		String requestconcatenatevideoUrl=httpUrl+"/requestconcatenatevideo";
		Map<String,String> map=new HashMap<String,String>();
		JhHttpClientUtil hd = new JhHttpClientUtil();
		map.put("session-key", sessionKey);
		map.put("media-info", mediaInfo);
        String msg= hd.post(requestconcatenatevideoUrl, map);
        Map<String,Object> resultMap=(Map<String,Object>)JSON.parseObject(msg);
        return  resultMap;
	}
	public Map<String,Object> vmsCheckconcatenatevideostatus(String httpUrl,String sessionKey,String concatenateRequestId){
		String checkconcatenatevideostatusUrl=httpUrl+"/checkconcatenatevideostatus";
		Map<String,String> map=new HashMap<String,String>();
		JhHttpClientUtil hd = new JhHttpClientUtil();
		map.put("session-key", sessionKey);
		map.put("concatenate-request-id", concatenateRequestId);
        String msg= hd.post(checkconcatenatevideostatusUrl, map);
        this.logger.info("vmsCheckconcatenatevideostatus:"+msg);
        Map<String,Object> resultMap=(Map<String,Object>)JSON.parseObject(msg);
        return resultMap;
	}
	//==================定制内容结束======================
}
